package com.pmcc.yn.service;

import com.pmcc.utils.AntdPage;
import com.pmcc.utils.BeanUtils;
import com.pmcc.utils.ResultBean;
import com.pmcc.yn.entity.AcceptInfo;
import com.pmcc.yn.entity.OrderList;
import com.pmcc.yn.entity.StoreInfo;
import com.pmcc.yn.entity.TechnicianInfo;
import com.pmcc.yn.repository.AcceptInfoRepository;
import com.pmcc.yn.repository.OrderListRepository;
import com.pmcc.yn.repository.StoreInfoRepository;
import com.pmcc.yn.repository.TechnicianInfoRepository;
import com.pmcc.yn.vo.AcceptInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by chuxuyang on 2019/1/12.
 */
@Service
@Transactional
public class AcceptInfoService {

     @Autowired
    AcceptInfoRepository repository;
     @Autowired
     OrderListRepository orderListRepository;
     @Autowired
     TechnicianInfoRepository technicianInfoRepository;

    public ResultBean showList(AntdPage antdPage , AcceptInfo model) throws Exception{
        ResultBean resultBean=new ResultBean();
        Page page=repository.findAll(this.getSpecification(model),antdPage.getPageRequest());

        resultBean.setPage(antdPage.getDefaultCurrent());
        resultBean.setTotal(page.getTotalElements());
        resultBean.setResultCode(ResultBean.SUCCESS);
        resultBean.setPageSize(antdPage.getPageSize());
        resultBean.setRows(page.getContent());
        return resultBean;
    }

    public Specification<AcceptInfo> getSpecification(AcceptInfo model) {
        return new Specification<AcceptInfo>() {
            @Override
            public Predicate toPredicate(Root<AcceptInfo> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                List<Predicate> list = new ArrayList<Predicate>();

                Predicate[] p = new Predicate[list.size()];
                return builder.and(list.toArray(p));
            }
        };
    }

    /**
     * 新增
     * @return
     */
    public ResultBean add(AcceptInfo model){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try{
            model.setCreateTime(new Date());

            repository.saveAndFlush(model);
            resultBean.setResultCode(ResultBean.SUCCESS);
            resultBean.setResultDesc("新增成功");
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }



    /**
     * 修改
     * @param map
     * @return
     */
    public ResultBean update(Map map) {
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
           AcceptInfo c = repository.findById(map.get("id").toString());
            BeanUtils.setBeanData(c,map);
            repository.saveAndFlush(c);
            resultBean.setResultCode(ResultBean.SUCCESS);
            resultBean.setObject(c);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public ResultBean deleteById(String id) {
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            repository.delete(id);
            resultBean.setResultCode(ResultBean.SUCCESS);

        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }
    public ResultBean myOrderByFlag(String id, int flag) {
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            List<AcceptInfo> list = repository.findByTechIdAndFlagOrderByCreateTimeDesc(id,flag);
            AcceptInfoVo vo = new AcceptInfoVo();
            List<AcceptInfoVo> voList = new ArrayList<>();
            for (AcceptInfo acceptInfo:list) {
                OrderList orderList = orderListRepository.findById(acceptInfo.getOrderId());
                //订单id
                vo.setId(orderList.getId());
                //赏金
                vo.setMoney(orderList.getMoney());
                //任务时间
                vo.setPreTime(orderList.getPreTime());
                //服务类型
                vo.setSerType(orderList.getSerType());
                //门店名称
                vo.setStoreName(orderList.getStoreName());
                //图片
                vo.setPic1(orderList.getVehPicl());
                voList.add(vo);
            }
            resultBean.setRows(voList);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }


    public ResultBean cancleOrder(String id,String techId,String message) {
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            AcceptInfo acceptInfo = repository.findByOrderIdAndTechId(id,techId);
            acceptInfo.setFlag(3);;//修改状态
            repository.saveAndFlush(acceptInfo);
            TechnicianInfo technicianInfo = technicianInfoRepository.findById(acceptInfo.getTechId());
            //扣该技师20积分
            technicianInfo.setIntegral(technicianInfo.getIntegral()-20);
            technicianInfoRepository.saveAndFlush(technicianInfo);
        }catch (Exception e){
            resultBean.setResultDesc(ResultBean.FAIL);

        }
        return resultBean;
    }

}
